package com.wcy.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcy.domain.AdminBank;
import com.wcy.domain.WorkIssue;
import com.wcy.model.R;
import com.wcy.service.WorkIssueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.naming.NamingEnumeration;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * <p>Title:WorkIssueController.java</p >
 * <p>Description: </p >
 * <p>Copyright: 公共服务与应急管理战略业务本部 Copyright(c)2020</p >
 * <p>Date:2021/9/27 20:53</p >
 *
 * @author wcy (wangchaoyanga@mail.taiji.com.cn)
 * @version 1.0
 */
@RestController
@RequestMapping("/workIssues")
@Api(tags = "客户工单的处理")
public class WorkIssueController {

    @Autowired
    private WorkIssueService workIssueService;

    @GetMapping
    @ApiOperation("分页查询客户工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页大小"),
            @ApiImplicitParam(name = "startTime",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",value = "结束时间"),
            @ApiImplicitParam(name = "status",value = "状态")
    })
    @PreAuthorize("hasAuthority('work_issue_query')")
    public R<Page<WorkIssue>> findByPage(@ApiIgnore Page<WorkIssue> page,String startTime,String endTime,Integer status){
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<WorkIssue> workIssuePage=workIssueService.findByPage(page,startTime,endTime,status);
        return R.ok(workIssuePage);
    }

    @PatchMapping
    @ApiOperation("回复客户工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "工单id"),
            @ApiImplicitParam(name = "answer",value = "回复内容")
    })
    @PreAuthorize("hasAuthority('work_issue_update')")
    public R answer(Long id,String answer){
        WorkIssue workIssue=new WorkIssue();
        workIssue.setId(id);
        workIssue.setAnswer(answer);
        SecurityContext context = SecurityContextHolder.getContext();
        String answerPersonId = context.getAuthentication().getName();
        workIssue.setAnswerUserId(Long.valueOf(answerPersonId));
        boolean update = workIssueService.updateById(workIssue);
        if(update){
            return R.ok();
        }
        return R.fail("回复失败");
    }

}
